package org.example.projectdemo.service;

import org.example.projectdemo.model.entity.Schedule;
import org.example.projectdemo.repository.ScheduleRepository;
import org.example.projectdemo.repository.AttendanceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

/**
 * ClassName: ScheduleSerivce
 * Package: org.example.projectdemo.service.impl
 * Description:
 *
 * @Author ZY
 * @Create 23:09
 * @Version 1.0
 */
@Service
public class ScheduleService {
    @Autowired
    private ScheduleRepository scheduleRepository;
    
    @Autowired
    private AttendanceRepository attendanceRepository;

    // 创建排课
    public Schedule createSchedule(Schedule schedule) {
        // 确保ID为null以创建新记录
        schedule.setId(null);
        return scheduleRepository.save(schedule);
    }

    // 更新排课
    public Schedule updateSchedule(Schedule schedule) {
        // 确保ID存在以更新记录
        if (schedule.getId() == null || !scheduleRepository.existsById(schedule.getId())) {
            throw new IllegalArgumentException("排课ID不存在，无法更新");
        }
        return scheduleRepository.save(schedule);
    }

    // 根据ID查找排课
    public Optional<Schedule> getScheduleById(Long id) {
        return scheduleRepository.findById(id);
    }

    // 检查排课是否存在
    public boolean existsScheduleById(Long id) {
        return scheduleRepository.existsById(id);
    }

    // 查找所有排课
    public List<Schedule> getAllSchedules() {
        return scheduleRepository.findAll();
    }

    // 根据课程ID查找排课
    public List<Schedule> getSchedulesByCourseId(Long courseId) {
        return scheduleRepository.findByCourseId(courseId);
    }

    // 根据日期查找排课
    public List<Schedule> getSchedulesByDate(LocalDate date) {
        return scheduleRepository.findByDate(date);
    }

    // 根据日期范围查找排课
    public List<Schedule> getSchedulesByDateBetween(LocalDate startDate, LocalDate endDate) {
        return scheduleRepository.findByDateBetween(startDate, endDate);
    }

    // 删除排课
    @Transactional
    public void deleteSchedule(Long id) {
        try {
            if (!scheduleRepository.existsById(id)) {
                throw new IllegalArgumentException("排课不存在，ID: " + id);
            }
            
            System.out.println("开始删除排课，ID: " + id);
            
            // 先删除相关的考勤记录
            System.out.println("删除排课相关的考勤记录，排课ID: " + id);
            attendanceRepository.deleteByScheduleId(id);
            
            // 再删除排课记录
            System.out.println("删除排课记录，ID: " + id);
            scheduleRepository.deleteById(id);
            
            System.out.println("排课删除完成，ID: " + id);
        } catch (Exception e) {
            System.err.println("删除排课失败，ID: " + id + ", 错误: " + e.getMessage());
            e.printStackTrace();
            throw new RuntimeException("删除排课失败: " + e.getMessage(), e);
        }
    }
}